/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package conversorcsvxml;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Marcel
 */
public class DocumentoXML {

    private ElementoXML doc;

    public DocumentoXML(ElementoXML doc) {
        this.doc = doc;
    }

    public void save() {
        OutputStream output = null;
        OutputStreamWriter writer = null;
        BufferedWriter bw = null;
        try {
            output = new FileOutputStream("notas.xml", false);
            writer = new OutputStreamWriter(output);
            bw = new BufferedWriter(writer);
            bw.write ("<"+ doc.nome+">\n");
            for (No linha : doc.filhos) {
                bw.write("<linha>\n");
                for (No celula : ((ElementoXML) linha).filhos) {
                    String nome = ((ElementoXML) celula).nome;
                    String valor = ((Valor) ((ElementoXML) celula).filhos.get(0)).literal;
                    bw.write("<"+nome+">"+valor+"</"+nome+">\n");
                }
            }
            bw.write("</"+doc.nome+">\n");
        }
        catch (FileNotFoundException f) {
            System.out.println("error1XML");
        }
        catch (IOException e) {
            System.out.println("error2XML");
        }
        finally {
            try {
                output.close();
                writer.close();
                //bw.close();
            } catch (IOException ex) {
                Logger.getLogger(DocumentoCSV.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
